<?php

class PositionController extends Controller
{

	/**
     * 过滤器
	 * @return 返回值是一个存放过滤器的数组
	 */
	public function filters()
	{
		return array(
            'accessAuth',//自定义授权方式
			//'accessControl', //默认授权方式
			'postOnly + delete',//删除操作只支持post请求
		);
	}
    /**
     * 自定义权限验证规则
     * @param type $filterChain
     * @throws CHttpException
     */
    public function filterAccessAuth($filterChain) {  
        if(Yii::app()->user->getIsGuest()) {  
            echo '<script>top.location.href="'.Yii::app()->user->returnUrl.'";</script>';
            exit;
        } else if(!Yii::app()->user->getIsGuest() && !in_array($this->getAction()->getId(), $this->authlessActions())){
            throw new CHttpException(403,'无权访问此页面');
        }
        $filterChain->run();  
    }  
    /**
     * 页面白名单
     * @return type
     */
    public function authlessActions() {  
        return array(
            'index','admin','view','create','update','delete','adminDictionary'
		); 
    }

	/**
	 * 查看页面
	 * @param integer $id
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * 添加页面
	 * 如果添加成功，跳转到查看页面
	 */
	public function actionCreate()
	{
		$model=new Position;

		//如果前台开启ajax则取消此注释
		//$this->performAjaxValidation($model);

		if(isset($_POST['Position']))
		{
			$model->attributes=$_POST['Position'];
                        $model->create_time=date('Y-m-d H:i:s',time());
                        $model->create_user=Yii::app()->user->username;
			if($model->save()){
                            Yii::app()->user->setFlash('success','操作成功！');
                            $this->redirect(array('Position/admin'));
                        }
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * 修改页面
	 * 如果修改成功，跳转到查看页面
	 * @param integer $id 
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		//如果前台开启ajax则取消此注释
		//$this->performAjaxValidation($model);

		if(isset($_POST['Position']))
		{
			$model->attributes=$_POST['Position'];
			if($model->save()){
                            Yii::app()->user->setFlash('success','操作成功！');
                            $this->redirect(array('Position/admin'));
                        }
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * 删除功能
	 * 一般只接受post方式的异步提交
	 * @param integer $id 
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isAjaxRequest && isset($_POST['id'])){
            $id=$_POST['id'];
            //获取id数组批量删除
            $criteria= new CDbCriteria;
            $criteria->addInCondition('id', $id);
            $res=Position::model()->deleteAll($criteria);
            //输出json结果
            echo CJSON::encode(array('result'=>$res));
        }else{
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        } 
	}

	/**
	 * 列表页
	 */
	public function actionAdmin()
	{
		$model=new Position('search');
		$model->unsetAttributes();  //清空并初始化
		if(isset($_GET['Position']))
			$model->attributes=$_GET['Position'];

		$this->render('admin',array(
			'model'=>$model,
			'data'=>$model->search(),
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Position the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Position::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Position $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='position-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	/**
	 * 列表页
	 */
	public function actionAdminDictionary()
	{
		$model=new Position('search');
		$model->unsetAttributes();  //清空并初始化
		if(isset($_GET['Position']))
			$model->attributes=$_GET['Position'];

		$this->render('adminDictionary',array(
			'model'=>$model,
			'data'=>$model->search(),
		));
	}        
}
